package eclipse.server;
import eclipse.lib.player.*;
import eclipse.lib.scripting.*;
import eclipse.lib.sockets.*;
import eclipse.server.threadpool.*;
import eclipse.util.*;
import eclipse.util.io.*;
/** Class that loads all of the Data for the Server. */
public class Loader 
{
	/** Initializes the Socket connections for the Server */
	public static void TCPInit()
	{
		Database.MAXPLAYERS	= Database.CLIENTNUM;
		Database.client = new ClientSocket[Database.CLIENTNUM];
		Database.player = new Player[Database.CLIENTNUM];
		Database.server = new ServerSocket(Database.PORTNUM, Database.client, Database.CLIENTTIMEOUT);
		for(int i = 0; i < Database.MAXPLAYERS; i++)
			Database.player[i] = new Player();
	}
	/** Processes any overwrites that might have been sent at startup */
	public static void ArgsProcess(String args[])
	{
		String value;
		for(int scan = 0; scan < args.length; scan++)
		{			
			if(args[scan].toLowerCase().startsWith("-netint="))
			{
				value = args[scan].toLowerCase().replace("-netint=", "").trim();
				Database.NETPROCESSINTERVAL = Integer.parseInt(value);
				System.out.println("Net Process Thread interval set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-clientnum="))
			{
				value = args[scan].toLowerCase().replace("-clientnum=", "").trim();
				Database.CLIENTNUM = Integer.parseInt(value);
				System.out.println("Client number set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-portnum="))
			{
				value = args[scan].toLowerCase().replace("-portnum=", "");
				Database.PORTNUM = Integer.parseInt(value);
				System.out.println("Port number set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-maxplayers="))
			{
				value = args[scan].toLowerCase().replace("-maxplayers=", "");
				Database.CLIENTNUM = Integer.parseInt(value);
				System.out.println("MAXPLAYERS number set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-maxitems="))
			{
				value = args[scan].toLowerCase().replace("-maxitems=", "");
				Database.MAXITEMS = Integer.parseInt(value);
				System.out.println("MAXITEMS number set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-maxclasses="))
			{
				value = args[scan].toLowerCase().replace("-maxclasses=", "");
				Database.MAXCLASSES = Integer.parseInt(value);
				System.out.println("MAXCLASSES number set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-maxnpcs="))
			{
				value = args[scan].toLowerCase().replace("-maxnpcs=", "");
				Database.MAXNPCS = Integer.parseInt(value);
				System.out.println("MAXNPCS number set to: " + value);			
			}
			else if(args[scan].toLowerCase().startsWith("-maxspells="))
			{
				value = args[scan].toLowerCase().replace("-maxspells=", "");
				Database.MAXSPELLS = Integer.parseInt(value);
				System.out.println("MAXSPELLS number set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-maxmaps="))
			{
				value = args[scan].toLowerCase().replace("-maxmaps=", "");
				Database.MAXMAPS = Integer.parseInt(value);
				System.out.println("MAXMAPS number set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-maxshops="))
			{
				value = args[scan].toLowerCase().replace("-maxshops=", "");
				Database.MAXSHOPS = Integer.parseInt(value);
				System.out.println("MAXSHOPS number set to: " + value);
			}
			else if(args[scan].toLowerCase().startsWith("-timeout="))
			{
				value = args[scan].toLowerCase().replace("-timeout=", "");
				Database.CLIENTTIMEOUT = Integer.parseInt(value);
				System.out.println("Client timeout set to: " + value);
			}
		}
	}	
	/** Launches the thread database */
	public static void ThreadInit()
	{
		Database.threadCache = new ThreadCache();
	}
	/** Loads the Scripting Engine */
	public static void ScriptInit()
	{
		JScript.makeObject("JavaScript", new JScript());
		JScript.makeObject("SadScript", new SadScript());
		JScript.makeObject("IOTools", new IOTools());
		JScript.addProcedure
		(
				"function importScript(script)" +
				"{" +
				"	JavaScript.importScript(SadScript.getERL(script));" +
				"}"
		);
		JScript.addProcedure("var svn = \"http://eclipse-mmorpgmaker.googlecode.com/svn/trunk/\";");		
		JScript.importScript(new ERL(Database.SCRIPTPATH).openStream());		
	}
}
